package com.tpo.jnotes.client.lib.component.dialog;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.TextAttribute;
import java.net.URI;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import com.tpo.jnotes.base.colors.BColors;
import com.tpo.jnotes.base.colors.schema.BCSSManager;
import com.tpo.jnotes.base.controls.BTransparentPanel;
import com.tpo.jnotes.base.resource.BResource;
import com.tpo.jnotes.client.lib.component.buttons.IconButton;
import com.tpo.jnotes.client.lib.language.Bundle;

public class NAboutDialog extends JDialog
{

    @SuppressWarnings( "unchecked" )
    public NAboutDialog()
    {
        setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        setUndecorated( true );
        getContentPane().setBackground( BColors.WHITE_D );
        setResizable( false );
        setPreferredSize( new Dimension( 380, 200 ) );
        setLocationRelativeTo( null );
        setModal( true );

        BTransparentPanel TopPanel = new BTransparentPanel();
        getContentPane().add( TopPanel, BorderLayout.CENTER );
        TopPanel.setLayout( new GridLayout( 4, 1, 0, 0 ) );

        BTransparentPanel panel = new BTransparentPanel( new BorderLayout( 0, 0 ) );
        TopPanel.add( panel );

        JLabel appLabel = new JLabel( "         jNotes" );
        panel.add( appLabel );
        appLabel.setHorizontalAlignment( SwingConstants.CENTER );
        appLabel.setForeground( BColors.BLUE_D );

        BTransparentPanel panel_1 = new BTransparentPanel();
        panel.add( panel_1, BorderLayout.EAST );

        JButton closeButton = new IconButton( BResource.getIcon( "/icons/close-icon.png" ), "EXIT" );
        closeButton.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                dispose();
            }
        } );
        panel_1.add( closeButton );

        JLabel licenseLabel = new JLabel( Bundle.getString( "about.license" ), SwingConstants.CENTER );
        licenseLabel.setForeground( BColors.BLUE_D );
        licenseLabel.setFont( BCSSManager.getCurrentCSS().getPanelFont() );
        TopPanel.add( licenseLabel );

        JLabel versionLabel = new JLabel( Bundle.getString( "about.version" ), SwingConstants.CENTER );
        versionLabel.setForeground( BColors.BLUE_D );
        versionLabel.setFont( BCSSManager.getCurrentCSS().getPanelFont() );
        TopPanel.add( versionLabel );

        final JLabel codeLabel = new JLabel( Bundle.getString( "about.code" ), SwingConstants.CENTER );

        Map attributes = BCSSManager.getCurrentCSS().getPanelFont().getAttributes();
        attributes.put( TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON );
        codeLabel.setFont( BCSSManager.getCurrentCSS().getPanelFont().deriveFont( attributes ) );

        codeLabel.addMouseListener( new MouseAdapter()
        {
            @Override
            public void mouseClicked( MouseEvent e )
            {
                try
                {
                    Desktop.getDesktop().browse( new URI( codeLabel.getText() ) );
                }
                catch( Exception ex )
                {
                    ex.printStackTrace();
                }
            }
        } );
        codeLabel.setForeground( BColors.BLUE_D );
        codeLabel.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ) );
        TopPanel.add( codeLabel );

        BTransparentPanel BottomPanel = new BTransparentPanel();
        BottomPanel.setBorder( new TitledBorder( new EmptyBorder( 0, 0, 0, 0 ), "Powered by",
            TitledBorder.CENTER, TitledBorder.TOP, null, BColors.BLUE_D ) );
        getContentPane().add( BottomPanel, BorderLayout.SOUTH );

        BottomPanel
            .add( new JLabel( "", BResource.getIcon( "/icons/about/java.png" ), SwingConstants.CENTER ) );
        BottomPanel.add( new JLabel( "", BResource.getIcon( "/icons/about/hibernate.png" ),
            SwingConstants.CENTER ) );
        BottomPanel.add( new JLabel( "", BResource.getIcon( "/icons/about/dropbox.png" ),
            SwingConstants.CENTER ) );
        BottomPanel.add( new JLabel( "", BResource.getIcon( "/icons/about/h2.png" ), SwingConstants.CENTER ) );
        BottomPanel.add( new JLabel( "", BResource.getIcon( "/icons/about/evernote.png" ),
            SwingConstants.CENTER ) );

    }

}
